package com.fas.app;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

public abstract class FreshActivity extends Activity implements OnClickListener {
	public final static int L = 30;
	public final static int M = 25;
	public final static int S = 20;
	public static int nowSize = M;
	protected ViewPager viewPager;
	protected ArrayList<View> pageViews;
	protected Button prev;
	protected Button next;
	protected int selectID;
	protected TextView allTitle, pages;
	protected GuidPageView view;
	public static ArrayList<Activity> list = new ArrayList<Activity>();
	public ArrayList<TextView> textViews = new ArrayList<TextView>();

	public int getSelectID() {
		return selectID;
	}

	public void setThisTitle(CharSequence text) {
		allTitle.setText(text);
		allTitle.setTextColor(Color.BLACK);
		allTitle.setTextSize(16.f);
	}

	public void ShowResultAcitvity(String s1, String s2, String s3) {
		Intent intent = new Intent(this, IllationResultActivity.class);
		intent.putExtra("result", s1);
		intent.putExtra("pathogeny", s2);
		intent.putExtra("control", s3);
		startActivity(intent);
	}

	public void setSelectID(int selectID) {
		this.selectID = selectID;
	}

	@Override
	protected void onResume() {
		super.onResume();
		setTextSize(nowSize);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case 0:
			setTextSize(S);
			break;
		case 1:
			setTextSize(M);
			break;
		case 2:
			setTextSize(L);
			break;
		}
		return super.onOptionsItemSelected(item);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		menu.add(0, 0, 1, "小号字体");
		menu.add(0, 1, 1, "中号字体");
		menu.add(0, 2, 1, "大号字体");
		return super.onCreateOptionsMenu(menu);

	}

	public void setTextSize(int size) {

		nowSize = size;
		Iterator<TextView> textview = textViews.iterator();
		while (textview.hasNext()) {
			TextView textView2 = (TextView) textview.next();
			textView2.setTextSize(nowSize);
		}
		view.refreshDrawableState();

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initAll();
	}

	public void initAll() {
		list.add(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guidpageview);
		viewPager = (ViewPager) findViewById(R.id.guidePages);
		prev = (Button) findViewById(R.id.register);
		next = (Button) findViewById(R.id.logon);
		allTitle = (TextView) findViewById(R.id.allTitle);
		pages = (TextView) findViewById(R.id.pages);
		pageViews = new ArrayList<View>();
		initialPageviews();
		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
		pages.setText("1/" + pageViews.size());
		view = ((GuidPageView) pageViews.get(0));
		view.setLeftInvisible(View.INVISIBLE);
		if (pageViews.size() == 1) {
			view.setRightInvisible(View.INVISIBLE);
		} else {
			view.setRightInvisible(View.VISIBLE);
		}
		next.setOnClickListener(this);
		viewPager.setOnClickListener(this);
		prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			setSelectID(arg0);

			if (arg0 == 0) {
				GuidPageView view = ((GuidPageView) pageViews.get(0));
				view.setLeftInvisible(View.INVISIBLE);
				if (pageViews.size() == 1) {
					view.setLeftInvisible(View.INVISIBLE);
				} else {
					view.setRightInvisible(View.VISIBLE);
				}

			} else if (arg0 == pageViews.size() - 1) {
				GuidPageView view = ((GuidPageView) pageViews.get(pageViews
						.size() - 1));
				view.setLeftInvisible(View.VISIBLE);
				view.setRightInvisible(View.INVISIBLE);
			} else {
				GuidPageView view = (GuidPageView) pageViews.get(arg0);
				view.setLeftInvisible(View.VISIBLE);
				view.setRightInvisible(View.VISIBLE);
			}
			pages.setText((arg0 + 1) + "/" + pageViews.size());
		}
	}

	public void addText(String title) {
		GuidPageView guide1 = new GuidPageView(this);
		TextView l = new TextView(this);
		l.setText(title);
		l.setTextSize(40);
		l.setTextColor(Color.BLACK);
		l.setGravity(Gravity.CENTER);
		guide1.setLeftInvisible(View.INVISIBLE);
		guide1.addView(l);
		pageViews.add(guide1);
	}

	public void addText(String title, float textSize, int color) {
		GuidPageView guide1 = new GuidPageView(this);
		TextView l = new TextView(this);
		textViews.add(l);
		l.setText(title);
		l.setGravity(Gravity.CENTER);
		l.setTextSize(nowSize);
		l.setTextColor(color);
		guide1.setLeftInvisible(View.INVISIBLE);
		guide1.addView(l);
		pageViews.add(guide1);
	}

	public void addText(String result, String pathogeny, String control,
			float textsize, int color) {
		GuidPageView guide1 = new GuidPageView(this);
		TextView l = new TextView(this);
		textViews.add(l);
		// l.setGravity(Gravity.CENTER_HORIZONTAL);
		l.setText(result);
		l.setTextSize(nowSize);
		l.setTextColor(Color.BLACK);
		guide1.addView(l);

		if (pathogeny != null) {
			TextView pathogenyText = new TextView(this);
			textViews.add(pathogenyText);
			pathogenyText.setText("病因：" + pathogeny);
			pathogenyText.setGravity(Gravity.LEFT);
			pathogenyText.setPadding(10, 0, 0, 0);
			pathogenyText.setTextSize(nowSize);
			pathogenyText.setTextColor(Color.BLACK);
			guide1.addView(pathogenyText);
		}

		if (control != null) {
			TextView controlText = new TextView(this);
			textViews.add(controlText);
			controlText.setText("防治：" + control);
			controlText.setGravity(Gravity.LEFT);
			controlText.setPadding(10, 0, 0, 0);
			controlText.setTextSize(nowSize);
			controlText.setTextColor(color);
			guide1.addView(controlText);
		}

		pageViews.add(guide1);
	}

	public void addImeageText(int imageId, String title, float textSize) {
		GuidPageView guide1 = new GuidPageView(this);
		TextView l = new TextView(this);
		textViews.add(l);

		l.setGravity(Gravity.CENTER);
		l.setTextSize(nowSize);
		l.setTextColor(Color.BLACK);
		ImageView iv = new ImageView(this);
		iv.setImageResource(imageId);
		l.setText(title);
		guide1.setLeftInvisible(View.INVISIBLE);
		guide1.addView(iv);
		guide1.addView(l);
		pageViews.add(guide1);
	}

	public void addTextImeage(int imageId, String title, float textSize,
			boolean setParentParam) {
		if (setParentParam) {
			GuidPageView guide1 = new GuidPageView(this);
			ScrollView.LayoutParams params = new ScrollView.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
			guide1.getMiddle_view().setLayoutParams(params);

			TextView l = new TextView(this);
			textViews.add(l);
			l.setTextSize(nowSize);
			l.setTextColor(Color.BLACK);
			ImageView iv = new ImageView(this);
			iv.setImageResource(imageId);
			l.setText(title);
			guide1.setLeftInvisible(View.INVISIBLE);
			guide1.addView(l);
			guide1.addView(iv);
			pageViews.add(guide1);
		} else {
			GuidPageView guide1 = new GuidPageView(this);
			TextView l = new TextView(this);
			textViews.add(l);

			l.setTextSize(nowSize);
			l.setTextColor(Color.BLACK);
			ImageView iv = new ImageView(this);
			iv.setImageResource(imageId);
			l.setText(title);
			guide1.setLeftInvisible(View.INVISIBLE);
			guide1.addView(l);
			guide1.addView(iv);
			pageViews.add(guide1);
		}
	}

	public void add2Text2Imeage(int imageId, String title, int imageId2,
			String title2, float textSize, boolean setParentParam) {
		GuidPageView guide1 = new GuidPageView(this);

		if (setParentParam) {
			ScrollView.LayoutParams params = new ScrollView.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
			guide1.getMiddle_view().setLayoutParams(params);
		}
		TextView l = new TextView(this);
		textViews.add(l);

		l.setTextSize(nowSize);
		l.setTextColor(Color.BLACK);

		ImageView iv = new ImageView(this);
		iv.setImageResource(imageId);
		l.setText(title);
		guide1.setLeftInvisible(View.INVISIBLE);

		TextView l2 = new TextView(this);
		textViews.add(l2);

		l2.setTextSize(nowSize);
		l2.setTextColor(Color.BLACK);

		ImageView iv2 = new ImageView(this);
		iv2.setImageResource(imageId2);
		l2.setText(title2);
		guide1.setLeftInvisible(View.INVISIBLE);

		guide1.addView(l);
		guide1.addView(iv);
		guide1.addView(l2);
		guide1.addView(iv2);

		pageViews.add(guide1);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	protected abstract void initialPageviews();
}
